package user

import (
	"wx-uniapp-go/internal/domain/errcode"
	"wx-uniapp-go/internal/logic/favorite_logic"
	"wx-uniapp-go/internal/repository/dto"
	"wx-uniapp-go/internal/server/echo"

	"github.com/gin-gonic/gin"
)

// CreateFavorite 创建收藏
func CreateFavorite(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID

	req := new(dto.CreateFavoriteReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := favorite_logic.NewFavoriteLogic(ctx).CreateFavorite(userId, req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// UpdateFavorite 更新收藏
func UpdateFavorite(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID

	req := new(dto.UpdateFavoriteReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := favorite_logic.NewFavoriteLogic(ctx).UpdateFavorite(userId, req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// DeleteFavorite 删除收藏
func DeleteFavorite(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID
	id := c.GetInt64("id")

	if id <= 0 {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := favorite_logic.NewFavoriteLogic(ctx).DeleteFavorite(userId, id); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// ListFavorite 查询收藏列表
func ListFavorite(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID

	req := new(dto.ListFavoriteReq)
	if err := c.ShouldBindQuery(req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	resp, err := favorite_logic.NewFavoriteLogic(ctx).ListFavorite(userId, req)
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c, resp)
}
